package com.isoftstone.service.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.isoftstone.dao.LoginDao;
import com.isoftstone.dao.MenuDao;
import com.isoftstone.dao.RoleDao;
import com.isoftstone.dao.UserDao;
import com.isoftstone.entity.Menu;
import com.isoftstone.entity.Role;
import com.isoftstone.entity.Users;
import com.isoftstone.service.RoleService;


@Service("RoleService")
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private MenuDao menuDao;
	@Autowired
	private LoginDao loginDao;
	@Override
	public List<Role> getAllRoleList() {
		// TODO Auto-generated method stub
		List<Role> list=roleDao.getAllRoleList();
		return list;
	}
	@Override
	public boolean insertRole(Role role) {
		// TODO Auto-generated method stub
		String role_id=role.getRole_id();
		String role_name=role.getRole_name();
		if(role_id!=null){
			boolean flag =roleDao.insertRole(role_id,role_name);
			if(flag){
				return true;
			}
		}
		return false;		
	}
	@Override
	public Role getRoleByRoleId(String role_id) {
		// TODO Auto-generated method stub
		Role role =roleDao.getRoleByRoleId(role_id);
		return role;
	}
	@Override
	public boolean editRole(Role role) {
		// TODO Auto-generated method stub
		String role_id=role.getRole_id();
		String role_name=role.getRole_name();
		if(role_id!=null){
			boolean flag =roleDao.editRole(role_id,role_name);
			if(flag){
				return true;
			}
		}
		return false;		
		
	}
	@Override
	public boolean deleteRole(String role_id) {
		// TODO Auto-generated method stub
		if(role_id!=null ){
			boolean flag =roleDao.deleteRole(role_id);
			if(flag){					
				return true;				
			}		
		}
		return false;
	}
	@Override
	public List<Menu> getAllMenuList() {
		// TODO Auto-generated method stub
		return menuDao.getAllMenuList();
	}
	@Override
	public List<Menu> getMenuListByRoleId(String role_id) {
		// TODO Auto-generated method stub
		List<Menu> menuList = new ArrayList<Menu>();
		menuList= loginDao.getParentMenuList(role_id);
		for(Menu menu:menuList){
			List<Menu> childrenMenuList=new ArrayList<Menu>();
			childrenMenuList=loginDao.getChildrenMenuListByParentAndRole(menu.getMenuId(),role_id);
			menu.setChildrenMenuList(childrenMenuList);
		}
		return menuList;
	}
	@Override
	public boolean editRoleMenu(String role_id, String[] menuIds) {
		// TODO Auto-generated method stub
		boolean flag=roleDao.deleteRoleMenu(role_id);
		if(true){
			boolean flag1=roleDao.insertRoleMenu(role_id,menuIds);
			return flag1;
		}
		return false;
	}		
}
